using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace QuanLyShopCD.BL
{
    class bl_ChoThue
    {
        Form f;
        PD.ProcessData con;
        String SQL;
        DataTable tb = new DataTable();
        public bl_ChoThue(Form f1)
        {
            this.f = f1;
            con = new PD.ProcessData();
        }

        public void Load_GridView()
        {
            SQL = "SELECT * FROM ThueDia";
            tb = con.GetTable(SQL);
            ((DataGridView)this.f.Controls["grv_chothue"]).DataSource = tb;
        }
        public void Hienthi()
        {
            DataGridView grv = (DataGridView)this.f.Controls["grv_chothue"];
            ComboBox cbb_MaKH = (ComboBox)this.f.Controls["cbb_MaKH"];
            ComboBox cbb_MaCD = (ComboBox)this.f.Controls["cbb_MaCD"];
            TextBox txt_NgayTra = (TextBox)this.f.Controls["txt_NgayTra"];
            TextBox txt_NgayMuon = (TextBox)this.f.Controls["txt_NgayMuon"];
            TextBox txt_SL = (TextBox)this.f.Controls["txt_SL"];

            cbb_MaKH.DataBindings.Clear();
            cbb_MaKH.DataBindings.Add("Text", grv.DataSource, "MAKH");
            cbb_MaCD.DataBindings.Clear();
            cbb_MaCD.DataBindings.Add("Text", grv.DataSource, "MaCD");

            txt_SL.DataBindings.Clear();
            txt_SL.DataBindings.Add("Text", grv.DataSource, "SoLuongMuon");
            txt_NgayMuon.DataBindings.Clear();
            txt_NgayMuon.DataBindings.Add("Text", grv.DataSource, "NgayMuon");
            txt_NgayTra.DataBindings.Clear();
            txt_NgayTra.DataBindings.Add("Text", grv.DataSource, "NgayTra");
        }

        public void Sua()
        {
            ComboBox cbb_MaKH, cbb_MaCD;
            cbb_MaKH = (ComboBox)this.f.Controls["cbb_MaKH"];
            cbb_MaCD = (ComboBox)this.f.Controls["cbb_MaCD"];
            TextBox txt_NgayTra = (TextBox)this.f.Controls["txt_NgayTra"];
            TextBox txt_NgayMuon = (TextBox)this.f.Controls["txt_NgayMuon"];
            TextBox txt_SL = (TextBox)this.f.Controls["txt_SL"];
            SQL = "UPDATE ThueDia SET SoLuongMuon='" + txt_SL.Text + "', NgayTra='" + txt_NgayTra.Text + "', NgayMuon='" + txt_NgayMuon.Text +"' WHERE MaKH='" + cbb_MaKH.SelectedValue.ToString() + "' AND MaCD='" + cbb_MaCD.SelectedValue.ToString() + "'";
            con.ExecuteSQL(SQL);
        }

        public void Load_Combobox_MaKH()
        {
            ComboBox cbb_MaKH = (ComboBox)this.f.Controls["cbb_MaKH"];
            tb = con.GetTable("select MaKH from KhachHang");
            cbb_MaKH.DataSource = tb;
            cbb_MaKH.DisplayMember = "MaKH";
            cbb_MaKH.ValueMember = "MaKH";
        }
        public void Load_Combobox_MACD()
        {
            ComboBox cbb_MaCD = (ComboBox)this.f.Controls["cbb_MaCD"];
            tb = con.GetTable("select MACD from SHOPCD");
            cbb_MaCD.DataSource = tb;
            cbb_MaCD.DisplayMember = "MaCD";
            cbb_MaCD.ValueMember = "MaCD";
        }
        public void Them()
        {
            TextBox txt_NgayTra, txt_NgayMuon, txt_SL;
            txt_NgayTra = (TextBox)this.f.Controls["txt_NgayTra"];
            txt_NgayMuon = (TextBox)this.f.Controls["txt_NgayMuon"];
            txt_SL = (TextBox)this.f.Controls["txt_SL"];
            ComboBox cbb_MaKH, cbb_MaCD;
            cbb_MaKH = (ComboBox)this.f.Controls["cbb_MaKH"];
            cbb_MaCD = (ComboBox)this.f.Controls["cbb_MaCD"];
            SQL = "INSERT INTO THUEDIA(MaKH, MaCD, SoLuongMuon, NgayMuon, NgayTra) VALUES('" + cbb_MaKH.SelectedValue.ToString() + "','" + cbb_MaCD.SelectedValue.ToString() + "'," + txt_SL.Text + ",'" + txt_NgayMuon.Text + "','" + txt_NgayTra.Text + "')";
            con.ExecuteSQL(SQL);
        }
        public void Xoa()
        {
            ComboBox cbb_MaKH, cbb_MaCD;
            cbb_MaKH = (ComboBox)this.f.Controls["cbb_MaKH"];
            cbb_MaCD = (ComboBox)this.f.Controls["cbb_MaCD"];
            SQL = "delete from ThueDia where MAKH ='" + cbb_MaKH.SelectedValue.ToString() + "' AND MACD='" + cbb_MaCD.SelectedValue.ToString() + "'";
            con.ExecuteSQL(SQL);
        }

        
    }
}
